import json

import sql
from websocket_information import WebSocketController
from requests import post


def get_devices(ws, uuid):
    try:
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        devices = sql.show_device()
        types = sql.show_type()
        td = dict(types)
        devices_list = []
        for device in devices:
            type_name = td[device[2]]
            device_info = {'device_id': device[0], 'device_name': device[1], 'type_id': device[2],
                           'type_name': type_name,
                           'device_addr': device[3]}
            devices_list.append(device_info)
            # print(device_info)
        data['message'] = devices_list
        article = json.dumps(data, ensure_ascii=False)
        print(article)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def get_device_by_id(ws, uuid, message):
    try:
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        device = sql.get_device_info(message)
        types = sql.show_type()
        td = dict(types)
        type_name = td[device[2]]
        device_info = {'device_id': device[0], 'device_name': device[1], 'type_id': device[2],
                       'type_name': type_name,
                       'device_addr': device[3]}
        data['message'] = device_info
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def add_device(ws, uuid, message):
    try:
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        name = message['device_name']
        type_id = message['type_id']
        device_addr = message['device_addr']
        res = sql.add_device(name, type_id, device_addr)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def update_device(ws, uuid, message):
    try:
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        d_id = message['device_id']
        device_name = message['device_name']
        type_id = message['type_id']
        device_addr = message['device_addr']
        res = sql.modify_device(d_id, device_name, type_id, device_addr)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def delete_device(ws, uuid, message):
    try:
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        res = sql.delete_device(message)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def status_equipment(ws, uuid, message):
    try:
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        device = sql.get_device_info(message)
        addr = str(device[3])
        addressee_url = addr + '/downEquipmentFunction'
        mes_info = {'sender': 'websocket', 'addresseeId': message, 'taskId': uuid,
                    'command': {'target': 'GetStatus'}}
        mes_info = json.dumps(mes_info)
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        data['message'] = None
        try:
            r = post(url=addressee_url, data=mes_info)
            if r.status_code == 200:
                data['message'] = r.json()
                print(r.json())
            else:
                data['message'] = {'data': {'status': 0}}
        except Exception as e:
            data['message'] = {'data': {'status': 0}}
            print(e)

        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)
